package com.ly.timer;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.ly.dao.UserTokenDao;
import com.ly.entity.User;
import com.ly.entity.UserToken;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
* 每1分钟扫描token是否过期
* 启动类需要 @EnableScheduling
* 作者:临渊
* 当前时间 2025/8/14 10:41
*/
@Component
public class TokenTimer {
    @Resource
    private UserTokenDao userTokenDao;

    @Scheduled(fixedDelay = 1000 * 60)
    public void tokenSet(){
        List<UserToken> userTokens = userTokenDao.selectAll(new UserToken());
        for(UserToken userToken:userTokens){
            if(userToken.getOverdue().compareTo(-1)>0){
                DateTime dateTime = DateUtil.offsetMinute(userToken.getCreateTime(), userToken.getOverdue());
                long time = dateTime.getTime();
                if(new Date().getTime()>time){
                    userTokenDao.deleteById(userToken.getId());
                }
            }
            System.out.println("定期删除token");
        }
    }
}
